package com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.Fragment_view;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.R;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.Fragment_persenter.PersonalCenterPersenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.fragment_contract.IPersonalCenterContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.AboutDeskMediaActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.ChangeLoginPasswordActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.LoginActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.MainActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.MyMassageActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.MyOrganizationActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view.PersonalInformationActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.MyToastUtils;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.SharedPreferencesUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/8/8.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 * <p>
 * 我的 碎片
 */

public class PersonalCenterFragment extends Fragment implements IPersonalCenterContract.IPersonalCenterView {
    @BindView(R.id.fragment_personal_center_iv_scanner)
    ImageView fragmentPersonalCenterIvScanner;
    @BindView(R.id.fragment_personal_center_iv_message)
    ImageView fragmentPersonalCenterIvMessage;
    @BindView(R.id.fragment_personal_center_top_re)
    RelativeLayout fragmentPersonalCenterTopRe;
    @BindView(R.id.fragment_personal_center_iv_head)
    ImageView fragmentPersonalCenterIvHead;
    @BindView(R.id.fragment_personal_center_tv_username)
    TextView fragmentPersonalCenterTvUsername;
    @BindView(R.id.fragment_personal_center_tv_job)
    TextView fragmentPersonalCenterTvJob;
    @BindView(R.id.fragment_personal_center_tv_my_group)
    TextView fragmentPersonalCenterTvMyGroup;
    @BindView(R.id.fragment_personal_center_re_my_group)
    RelativeLayout fragmentPersonalCenterReMyGroup;
    @BindView(R.id.fragment_personal_center_tv_only)
    TextView fragmentPersonalCenterTvOnly;
    @BindView(R.id.fragment_personal_center_only_line)
    View fragmentPersonalCenterOnlyLine;
    @BindView(R.id.fragment_personal_center_re_technical_support)
    RelativeLayout fragmentPersonalCenterReTechnicalSupport;
    @BindView(R.id.fragment_personal_center_iv_switch)
    ImageView fragmentPersonalCenterIvSwitch;
    @BindView(R.id.fragment_personal_center_re_only)
    RelativeLayout fragmentPersonalCenterReOnly;
    @BindView(R.id.fragment_personal_center_re_change_login_password)
    RelativeLayout fragmentPersonalCenterReChangeLoginPassword;
    @BindView(R.id.fragment_personal_center_re_exit)
    RelativeLayout fragmentPersonalCenterReExit;
    Unbinder unbinder;
    private PersonalCenterPersenter personalCenterPersenter = new PersonalCenterPersenter(this);
    private Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_personal_center, container, false);
        unbinder = ButterKnife.bind(this, view);
        personalCenterPersenter.initView(mContext,getActivity());
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        String  imageUrl = (String) SharedPreferencesUtils.getInstance(mContext).getDate("imageUrl","String");
        String  realName = (String) SharedPreferencesUtils.getInstance(mContext).getDate("realName","String");
        String  deptName = (String) SharedPreferencesUtils.getInstance(mContext).getDate("deptName","String");
        String  careerId = (String) SharedPreferencesUtils.getInstance(mContext).getDate("careerId","String");
        Log.e("onStart", "onStart: "+ imageUrl);
        Glide.with(mContext).load(imageUrl).placeholder(R.mipmap.menu_head_defalt).error(R.mipmap.menu_head_defalt).into(fragmentPersonalCenterIvHead);
        fragmentPersonalCenterTvUsername.setText(realName);
        if(deptName != null && !TextUtils.isEmpty(deptName)){
            if(careerId != null && !TextUtils.isEmpty(careerId)){
                fragmentPersonalCenterTvJob.setText(deptName+"-"+careerId);
            }else{
                fragmentPersonalCenterTvJob.setText("IT技术部-Web前端开发工程师");
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.fragment_personal_center_iv_scanner, R.id.fragment_personal_center_iv_message,
            R.id.fragment_personal_center_iv_head, R.id.fragment_personal_center_re_my_group,
            R.id.fragment_personal_center_re_technical_support, R.id.fragment_personal_center_iv_switch,
            R.id.fragment_personal_center_re_exit,R.id.fragment_personal_center_re_change_login_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fragment_personal_center_iv_scanner:
                personalCenterPersenter.onClickedImageViewScanner();
                break;
            case R.id.fragment_personal_center_iv_message:
                personalCenterPersenter.onClickedImageViewMessage();
                break;
            case R.id.fragment_personal_center_iv_head:
                personalCenterPersenter.onClickedImageVieHead();
                break;
            case R.id.fragment_personal_center_re_my_group:
                personalCenterPersenter.onClickedReMyGroup();
                break;
            case R.id.fragment_personal_center_re_technical_support:
                personalCenterPersenter.onClickedReTechnicalSupport();
                break;
            case R.id.fragment_personal_center_iv_switch:
                personalCenterPersenter.onClickedImageViewSwitch();

                break;
            case R.id.fragment_personal_center_re_exit:
             //   personalCenterPersenter.onClickedReExit(mContext);
                // 先弹出退出登录提示框
                personalCenterPersenter.showLogoutDialog(mContext);
                break;
            case R.id.fragment_personal_center_re_change_login_password:
                personalCenterPersenter.onClickedChangePassword();
                break;
        }
    }

    @Override
    public void switchOpen() {
        fragmentPersonalCenterIvSwitch.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.personal_open));

    }

    @Override
    public void switchClose() {
        fragmentPersonalCenterIvSwitch.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.personal_close));
    }

    @Override
    public void toStartActivityForResult(Intent intent, int code) {
        startActivityForResult(intent,code);
    }

    @Override
    public void toMyOrganizationPager() {
        startActivity(new Intent(mContext,MyOrganizationActivity.class));
    }

    @Override
    public void toMessagePager() {
        startActivity(new Intent(mContext,MyMassageActivity.class));
    }

    @Override
    public void toTechnicalSupportPager() {
        startActivity(new Intent(mContext,AboutDeskMediaActivity.class));
    }

    @Override
    public void toPersonalInformationPager() {
        startActivity(new Intent(mContext,PersonalInformationActivity.class));
    }

    @Override
    public void toChangePasswordPager() {
        startActivity(new Intent(mContext,ChangeLoginPasswordActivity.class));
    }

    @Override
    public void toLoginPager() {
        Intent  intent = new Intent(mContext,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void showToastInfo(String message) {
        MyToastUtils.ShortToaseShow(mContext,message);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        personalCenterPersenter.onActivityResult(requestCode,resultCode,data);
    }
}
